// Copyright 2020 Poofee (https://github.com/Poofee)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
/*****************************************************************************
 *                                                                           *
 *  File:    coil3d.cpp                                                      *
 *                                                                           *
 *****************************************************************************
 *                                                                           *
 *  Authors: Poofee                                                          *
 *  Email:   poofee@qq.com                                                   *
 *  Address: https://github.com/Poofee                                       *
 *  Date:    2020年08月10日                                                   *
 *                                                                           *
 *****************************************************************************/
#include "coil3d.h"

Coil3D::Coil3D() {

}


void Coil3D::init() {
	initCoil();
	//initPM();
	//initCoil1();
}

/*!
 * @brief      测试永磁模型
 * @author     Poofee
 * @date       2020年8月29日
 * @param[out] 
 * @return     void  
**/
void Coil3D::initPM() {
	/** 文件名，需不带扩展名 **/
	setFileName((char*)"../geo/pm/magnets");
	/** 初始化时间步长 **/
	current_step = 0;
	min_time = 0;
	max_time = 0;
	timesteps.resize(1);

	/** 初始化变量 **/
	current_step = 0;
	initPhysicalVariables(timesteps.size());

	MAX_NONLINEARSTEPS = 100;
	/** 设置材料参数 **/
	CMaterial* air_material = new CMaterial;
	materialList.emplace_back(air_material);


	CMaterial* pm_material = new CMaterial;
	materialList.emplace_back(pm_material);
	
	PermanentMagnet* pm = new PermanentMagnet;
	pm->type = 0;
	pm->mur = 4;
	pm->Br = 4;
	pm->Hc = pm->Br / miu0 / pm->mur;
	pm->v[0] = 0;
	pm->v[1] = -1;
	pm->v[2] = 0;
	pm_material->pm = pm;

	//pm_material->H_c = pm->Hc;
	pm_material->miu *= pm->mur;


	/** 设置映射关系 **/
	setMaterial(1, pm_material);/** 线圈 **/
	setMaterial(2, air_material);/** 空气 **/
	/** 设置形变区域 **/
	tag_xiantie = 1;
	tag_air = 2;

	Precision = 1e-6;
}


/*!
 * @brief      测试线圈模型
 * @author     Poofee
 * @date       2020年8月29日
 * @param[out] 
 * @return     void  
**/
void Coil3D::initCoil() {
	/** 文件名，需不带扩展名 **/
	setFileName((char*)"../geo/coil/coil3d");
	/** 初始化时间步长 **/
	current_step = 0;
	min_time = 0;
	max_time = 0;
	timesteps.resize(1);

	/** 初始化变量 **/
	current_step = 0;
	initPhysicalVariables(timesteps.size());

	MAX_NONLINEARSTEPS = 100;
	/** 设置材料参数 **/
	CMaterial* air_material = new CMaterial;
	materialList.emplace_back(air_material);


	CMaterial* coil_material1 = new CMaterial;
	materialList.emplace_back(coil_material1);
	coil_material1->tau = 60 / (3e-3 * 17e-3);
	Coil* coil1 = new Coil;
	coil1->xyz = 1;
	coil1->Nc = 60;
	coil1->Rc = 2;
	coil1->type = CICULARCOIL;
	coil1->height = 2.5e-3;
	coil1->center[0] = 0;
	coil1->center[1] = 0e-3;
	coil1->center[2] = 0e-3;
	coil1->r0 = 0.85e-3;
	coil1->width = 0.4e-3;
	coil_material1->coil = coil1;
	coil1->Jor = 1;// 28 / coil1->Rc * coil1->Nc / coil1->width / coil1->height;


	/** 设置映射关系 **/
	setMaterial(1, coil_material1);/** 线圈 **/
	setMaterial(2, air_material);/** 空气 **/
	/** 设置形变区域 **/
	setXiantieTag(1);
	setAirTag(2);

	Precision = 1e-6;

	unitRatio = 1;
}


/*!
 * @brief      
 * @author     Poofee
 * @date       2020年9月8日
 * @param[out] 
 * @return     void  
**/
void Coil3D::initCoil1() {
	/** 文件名，需不带扩展名 **/
	setFileName((char*)"../geo/coil/coil3d1");
	/** 初始化时间步长 **/
	current_step = 0;
	min_time = 0;
	max_time = 0;
	timesteps.resize(1);

	/** 初始化变量 **/
	current_step = 0;
	initPhysicalVariables(timesteps.size());

	MAX_NONLINEARSTEPS = 100;
	/** 设置材料参数 **/
	CMaterial* air_material = new CMaterial;
	materialList.emplace_back(air_material);


	CMaterial* coil_material1 = new CMaterial;
	materialList.emplace_back(coil_material1);
	coil_material1->tau = 60 / (3e-3 * 17e-3);
	Coil* coil1 = new Coil;
	coil1->xyz = 1;
	coil1->Nc = 60;
	coil1->Rc = 2;
	coil1->type = CICULARCOIL;
	coil1->height = 0.5e-3;
	coil1->center[0] = 0;
	coil1->center[1] = 0e-3;
	coil1->center[2] = 0e-3;
	coil1->r0 = 1e-3;
	coil1->width = 0.4e-3;
	coil_material1->coil = coil1;
	coil1->Jor = 28 / coil1->Rc * coil1->Nc / coil1->width / coil1->height;


	/** 设置映射关系 **/
	setMaterial(1, coil_material1);/** 线圈 **/
	setMaterial(2, air_material);/** 空气 **/
	/** 设置形变区域 **/
	tag_xiantie = 1;
	tag_air = 2;

	Precision = 1e-6;
}

void Coil3D::run() {
	init();
	openGeo();

    MagnetoStatic3DNR();

	/** 输出信息 **/
	outputResults();
}
